package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;

import longcaisuyun.longcai.com.net.PostEditPassword;
import wh.utils.StringUtils;



public class MimaActivity extends Activity implements View.OnClickListener {

    LinearLayout fanhui_lin;
    EditText et_old, et_new, et_new1;
    Button bt_ok;
    Context context;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context=MimaActivity.this;
        setContentView(R.layout.activity_mima);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
    }

    private void ViewInit() {
        fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(this);
        et_old = (EditText) findViewById(R.id.et_old);

        et_new = (EditText) findViewById(R.id.et_new);
        et_new1 = (EditText) findViewById(R.id.et_new1);
        bt_ok = (Button) findViewById(R.id.bt_ok);
        bt_ok.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:
                MimaActivity.this.finish();
                break;
            case R.id.bt_ok:
                xiuGaiMima();
                break;
        }
    }
    public  void xiuGaiMima() {
        if (et_old.getText().toString().length()<6){
            Toast.makeText(context, "请输入完整的旧密码", Toast.LENGTH_SHORT).show();
        }else if (StringUtils.isEmpty(et_old.getText().toString())){
            Toast.makeText(context, "请输入旧密码", Toast.LENGTH_SHORT).show();
        }else if(StringUtils.isEmpty(et_new.getText().toString())){
            Toast.makeText(context, "请输入新密码", Toast.LENGTH_SHORT).show();
        }else if (et_old.getText().toString().equals(et_new.getText().toString())){
            Toast.makeText(context, "新旧密码不能相同", Toast.LENGTH_SHORT).show();
        }

        else if (et_new.getText().length()<6) {
            Toast.makeText(context, "请输入6-16位新密码", Toast.LENGTH_SHORT).show();
        }else if (StringUtils.isEmpty(et_new1.getText().toString())){
            Toast.makeText(context, "请输入确认密码", Toast.LENGTH_SHORT).show();
        }else if (et_new.getText().length()<6){
            Toast.makeText(context, "请输入6-16位新密码", Toast.LENGTH_SHORT).show();
        }else if (!et_new.getText().toString().equals(et_new1.getText().toString())){
            Toast.makeText(context, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        }else{
            postEditPassword();
        }

    }
    //接口修改密码
    private  void postEditPassword() {
        MyApplication.myviewutil.startProgressDialog(MimaActivity.this);
        Log.e("司机的ID",MyApplication.myPreferences.readUid());
        new PostEditPassword(MyApplication.myPreferences.readUid(),
                et_old.getText().toString(),et_new.getText().toString(),"1",new AsyCallBack<PostEditPassword.Info>(){
            @Override
            public void onSuccess(String toast, int type, PostEditPassword.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                switch(info.message){
                    case "0":
                        Toast.makeText(MimaActivity.this, "密码修改失败,请重试", Toast.LENGTH_SHORT).show();
                        break;
                    case "1":
                        Toast.makeText(MimaActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                        break;
                    case "2":
                        Toast.makeText(MimaActivity.this, "旧密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                        break;
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(MimaActivity.this, "修改失败，请检查您的网络后再进行尝试", Toast.LENGTH_SHORT).show();
            }
        }).execute(MimaActivity.this);

    }

}



